package com.my.yc.d0301;

import javax.servlet.*;
import javax.servlet.http.*;
import javax.servlet.annotation.*;
import java.io.IOException;

@WebServlet(name = "AddDataServlet", value = "/AddDataServlet")
public class AddDataServlet extends HttpServlet {
    @Override
    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {

        /*会话丢失  默认是通过 cookie 保存会话id
        * 1. 更换浏览器   不同浏览器 会开启不同的会话
        * 2. 关闭浏览器   会话id丢失 ==> 会话失效  会话还在进行 但 id 丢失了
        * 3. 会话自动失效  时效性
        *                   tomcat的默认失效时间是 30 分钟
        * 4. 会话手动的销毁
        * */

        request.setCharacterEncoding("utf-8");
        response.setCharacterEncoding("utf-8");
        response.setContentType("text/html;charset=utf-8");

        String data = request.getParameter("data");
        // 将数据保存到请求对象中
        request.setAttribute("data", data);
        // 从请求中获取会话对象
        HttpSession session = request.getSession();
        session.getId();// 会话ID 唯一值 与会话一一对应
        // 将数据保存到会话对象中
        session.setAttribute("data",data);

        response.getWriter().printf("数据保存成功！！");
    }

    @Override
    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {

    }
}
